import { map } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { ApiService } from '@core/services/http/api.service';
import { ApesModalService } from '@shared/apes/modal';
import { ApesMessageService } from '@shared/apes/message';
import { ApesInputFormComponent } from '@shared/apes-input-form/apes-input-form.component';
import { deepClone } from '../../utils/utils';
import { ExpressionServiceSimple } from '@core/services/expression/ExpressionService-simple';
import { toBoolean } from '@shared/apes/core';

@Injectable()
export class DialogInputService {
  constructor(private apiService: ApiService,
              private msg: ApesMessageService,
              private modalService: ApesModalService,
              protected expressionService: ExpressionServiceSimple) {
  }

  openDialog(url: string, callback: any, dialog?: any, option?: any): Observable<any> {
    return this.apiService.post('form.get', { 'body': JSON.stringify({ 'id': url }) }).pipe(
      map((body) => {
        body.result = [];
        let footerBtn = false;
        let footer: any[];
        if (option) {
          body['conditionModel'] = option['conditionModel'];
          body['formId'] = option['formId'];
          body['width'] = option['width'];
          body['apesClosable'] = option.hasOwnProperty('apesClosable') ? toBoolean(option['apesClosable']) : true;
          body['apesMaskClosable'] = option.hasOwnProperty('apesMaskClosable') ? toBoolean(option['apesMaskClosable']) : true;
          if (option.hasOwnProperty('footerBtn')) {
            footer = deepClone(option.footerBtn);
            footerBtn = true;
          } else {
            body['okText'] = option['okText'];
            body['cancelText'] = option['cancelText'];
          }
        }
        const openDialog = dialog ? dialog : ApesInputFormComponent;

        let title = option && option.title ? option.title : body.title ? body.title : body.string;

        if (footerBtn) {
          footer.forEach((btn) => {
            btn.loading = false;
            if (btn.value == 'cancel') {
              btn.onClick = () => {
                this.modalService.closeAll();
              };
            } else {
              btn.loading = (data) => {
                return data.isLoading;
              };
              btn.onClick = (data) => {
                data['isLoading'] = true;
                let expression = btn.callback;
                return this.expressionService.evaluate(expression, {
                  context: data.ctx,
                  data: data,
                  node: btn,
                  api: this.apiService,
                }, true);
              };
            }
          });

          const dialogRef = this.modalService.create({
            apesWidth: body.width ? body.width : '50vw',//800
            apesTitle: title,
            apesContent: openDialog,
            apesClosable: body.apesClosable,
            apesMaskClosable: body.apesMaskClosable,
            apesComponentParams: body,
            apesFooter: footer,
          });
          return dialogRef.afterClose.subscribe((result) => callback(result));

        } else {

          let okText = body['okText'] ? body['okText'] : '确定';
          let cancelText = body['cancelText'] ? body['cancelText'] : '取消';
          const dialogRef = this.modalService.create({
            apesWidth: body.width ? body.width : '50vw',//800
            apesTitle: title,
            apesContent: openDialog,
            apesClosable: body.apesClosable,
            apesMaskClosable: body.apesMaskClosable,
            apesComponentParams: body,
            apesFooter: [
              {
                label: cancelText,
                type: 'default',
                onClick: () => {
                  dialogRef.close();
                },
              },
              {
                label: okText,
                type: 'primary',
                disabled: (componentInstance) => {
                  let check = false;
                  if (componentInstance['form'].hasOwnProperty('customValid')) {
                    check = componentInstance['form'].customValid;
                  } else {
                    check = !componentInstance['form'].valid;
                  }
                  return check;
                },
                onClick: (componentInstance) => {
                  dialogRef.destroy(componentInstance['resultData']);
                },
              },
            ],
          });
          return dialogRef.afterClose.subscribe((result) => callback(result));
        }

      }));
  }

}
